package com.hy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hy.common.Result;
import com.hy.entity.Lesson;
import com.hy.service.LessonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/lessons")
public class LessonController {

    @Autowired
    private LessonService lessonService;

    /**
     * 获取所有课程
     */
    @GetMapping
    public Result<List<Lesson>> getAllLessons() {
        return Result.success(lessonService.getAllLessons());
    }

    /**
     * 根据ID获取课程
     */
    @GetMapping("/{id}")
    public Result<Lesson> getLesson(@PathVariable Long id) {
        return Result.success(lessonService.getLessonById(id));
    }

    /**
     * 添加课程
     */
    @PostMapping
    public Result<Boolean> addLesson(@RequestBody Lesson lesson) {
        return Result.success(lessonService.addLesson(lesson));
    }

    /**
     * 修改课程
     */
    @PutMapping("/{id}")
    public Result<Boolean> updateLesson(@PathVariable Long id, @RequestBody Lesson lesson) {
        lesson.setId(id.intValue());
        return Result.success(lessonService.updateLesson(lesson));
    }

    /**
     * 删除课程
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> deleteLesson(@PathVariable Long id) {
        return Result.success(lessonService.deleteLesson(id));
    }

    /**
     * 根据教师ID获取课程列表
     */
    @GetMapping("/teacher/{teacherId}")
    public Result<List<Lesson>> getLessonsByTeacherId(@PathVariable Long teacherId) {
        return Result.success(lessonService.getLessonsByTeacherId(teacherId));
    }

    // 分页条件查询课程
    @GetMapping("/admin/page")
    public Result<Page<Lesson>> pageLessons(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String lessonName,
            @RequestParam(required = false) Integer teacherId
    ) {
        Page<Lesson> page = lessonService.pageLessons(pageNum, pageSize, lessonName, teacherId);
        return Result.success(page);
    }

    // 新增课程
    @PostMapping("/admin/add")
    public Result addLessonAdmin(@RequestBody Lesson lesson) {
        lesson.setCreateTime(new Date());
        lesson.setUpdateTime(new Date());
        boolean success = lessonService.save(lesson);
        return success ? Result.success() : Result.error(500, "新增精品课程失败");
    }

    // 更新课程
    @PutMapping("/admin/update")
    public Result updateLesson(@RequestBody Lesson lesson) {
        lesson.setUpdateTime(new Date());
        boolean success = lessonService.updateById(lesson);
        return success ? Result.success() : Result.error(500, "更新精品课程失败");
    }

    // 删除课程
    @DeleteMapping("/admin/{id}")
    public Result deleteLesson(@PathVariable Integer id) {
        boolean success = lessonService.removeById(id);
        return success ? Result.success() : Result.error(500, "删除精品课程失败");
    }

    // 单个查询课程
    @GetMapping("/admin/{id}")
    public Result<Lesson> getLesson(@PathVariable Integer id) {
        Lesson lesson = lessonService.getById(id);
        return lesson != null ? Result.success(lesson) : Result.error(404, "未找到对应精品课程");
    }
}
